<?php

define("E_SQL_DUPLICATE_KEY", 1062 );
define("E_SQL_DUPLICATE_ENTRY", 1022 );

$dup_sql_errors = array(E_SQL_DUPLICATE_KEY, E_SQL_DUPLICATE_ENTRY);

function CheckMySQL ( $res, $file = "", $line = "" , $sql = "" ){
	if ( ! $res ){
	    echo "<p><H3>SQL error</H></p>";
		echo "<p><H4>Error ". mysql_errno() . ", '" . mysql_error() . "'</H></p>";
		echo "<p>at $file:$line</p><!-- Query:\n$sql\n -->";
		die ();
	}
	return $res;
}

class Db{
// public

	// contructor
	function Db ( $host, $database, $user, $password ){
		$this -> host = $host;
		$this -> database = $database;
		$this -> user = $user;
		$this -> password = $password;
    }

	function DbConnect(){
		global $encoding, $db_type, $common_data_server;

		$this -> link = mysql_connect ( $this -> host, $this -> user, $this -> password );
		if ( !$this -> link )
			die ( "<p><H4>Can't connect to SQL server</p>" );
		if ($encoding == 'UTF8'){
			mysql_query("SET CHARACTER SET utf8", $this -> link);
			mysql_query("SET NAMES 'utf8'", $this -> link);
		}else{
			mysql_query("SET CHARACTER SET cp1251", $this -> link);
			mysql_query("SET NAMES 'cp1251'", $this -> link);
		}
		CheckMySQL ( @mysql_select_db ( $this -> database, $this -> link ), __FILE__, __LINE__ );
	}

	// returns an object of the Select class
	function Select ( $query ){
		if(!$this -> link)$this->DbConnect();
		$res = CheckMySQL ( @mysql_query ( $query, $this -> link ) ,"","",$query);
		return new Select ( $res, $this -> link );
	}

	// returns numbers of affected rows.
	// in case of error, returns -1
	function ExecSQL ( $query, $can_die = true ){
		if(!$this -> link)$this->DbConnect();
		$res = @mysql_query ( $query, $this -> link );
		if ( $can_die )
			CheckMySQL ( $res ,"","",$query);
		return mysql_affected_rows ( $this -> link );
	}

	// returns last error number
	function Errno(){
		return mysql_errno($this -> link );
	}

	// returns full text of the last error
	function ErrorText(){
		return mysql_error($this -> link );
	}

	function GetInsertID(){
		return @mysql_insert_id( $this -> link );
	}

	function Commit(){
	    return;
	}

// private

	var $host;
	var $database;
	var $user;
	var $password;
	var $link;
}



//
class Select{
// public

	var $RowCount; // a number of selected rows

	function Select ( $query_result, $link ){
		$this -> link = $link;
		$this -> result = $query_result;
		$this -> RowCount = mysql_num_rows ( $this -> result );
	}

	// Fetch a result row as an associative array
	function FetchArray(){
		return @mysql_fetch_array ( $this -> result );
	}

	function FetchRow (){
    	return  @mysql_fetch_row ($this -> result );
	}

	function FieldNames(){
		$result = array();
		$count = @mysql_num_fields ( $this -> result );
		for ( $i=0; $i < $count; $i++){
		    array_push( $result, @mysql_field_name ( $this -> result , $i));
		}
		return $result;
	}

	function DataSeek($RowNumber){
		return @mysql_data_seek( $this -> result ,  $RowNumber);
	}

	function FieldType($FieldNumber){
	    return @mysql_field_type( $this -> result ,  $FieldNumber);
	}

	function FreeResult(){
		@mysql_free_result( $this -> result );
	}

// private

    var $result;
}

?>